/**
 * Setvor project
 */
package setvor.npc;

import java.io.*;

/**
 * @author julatec
 *
 */
@LibraryReference(libraryPath =  "lib", libraryName = "npc")
public abstract class NativeClass implements NativeWriteable{
	
	protected final long pointer;
	
	protected NativeClass() throws Exception{
		this.pointer = nativeNew();
	}
	
	/**
	 * Returns the long value of the pointer.
	 * @return the pointer
	 */
	public long getPuntero() {
		return pointer;
	};
	
	/**
	 * Create a new instance of the class
	 * @return the pointer to the instance.
	 */
	protected abstract long nativeNew() throws Exception;

	/**
	 * Releases the memory in the external library.
	 */
	protected abstract void nativeFinalize() throws Exception;

	/**
	 * This method override the finalize for release the pointer
	 * in the native library.
	 */
	protected void finalize(){
		try {
			nativeFinalize();
			super.finalize();
		} catch (Throwable e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * This is path of the library.
	 */
	private static final String LIBRARY_PATH = "lib";
	
	/**
	 * This is the name of the library.
	 */
	private static final String LIBRARY_NAME="npc";
	
	/**
	 * This method loads the native library 
	 * basic functions
	 */
	private native static void loadLibrary();
	
	/**
	 * This method release the resources in the native
	 * library enviroment.
	 */
	public native static void unloadLarary();
	
	/**
	 * Load the basic functions. 
	 */
	static{
		String libraryFullPath=
			new File(LIBRARY_PATH+File.separator+System.mapLibraryName(LIBRARY_NAME)).getAbsolutePath();

		System.load(libraryFullPath);

		loadLibrary();
	}
	

}
